#!/bin/bash

################################################
######### converting DICOM to NIfTI ############
######### for VASO and BOLD runs    ############
######### applies to SIEMENS scanners ##########
######### with versions VE and higher ##########
################################################

# this script uses dcm2niix by Chris Rorden 
# which is available here: https://www.nitrc.org/projects/dcm2nii/


#generating folder to put nii files in to, if it doent exist yet

mkdir ./nii

# going into each folder with DICOM files and converting them to NIfTI
for dir in ./*/
do
dir=${dir%*/}
cd ./$dir
echo  ${dir##*/}

# including the series name into the file name
dcm2niix -ba y -z y -o ../nii/ -f S%s_%d_e%e ./

cd ..
done 

# done converting 
# now lets rename the file with the same nameing snipet as 
# BIDS woudl want it: 

cd nii

for file in *.nii.gz ; do mv $file ${file//_S00/_cbv} ; done
for file in *.json ; do mv $file ${file//_S00/_cbv} ; done

for file in *.nii.gz ; do mv $file ${file//_S01/_bold} ; done
for file in *.json ; do mv $file ${file//_S01/_bold} ; done

for file in *.nii.gz ; do mv $file ${file//_e1/} ; done
for file in *.json ; do mv $file ${file//_e1/} ; done

for file in *.nii.gz ; do mv $file ${file//_E00/} ; done
for file in *.json ; do mv $file ${file//_E00/} ; done

for file in *.nii.gz ; do mv $file ${file//_P_ph/_phase1} ; done
for file in *.json ; do mv $file ${file//_P_ph/_phase1} ; done

for file in *.nii.gz ; do mv $file ${file//_M/_magnitude1} ; done
for file in *.json ; do mv $file ${file//_M/_magnitude1} ; done






 
